﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace Playlist_file_loader
{

    public partial class MainForm : Form
    {

        //-------------------------------------------------------------------//
        //-------------------------member variables--------------------------//
        //-------------------------------------------------------------------//

        public delegate void MethodUpdateProgressBar(int value);
        public MethodUpdateProgressBar methodUpdateProgressbar;

        //public delegate void Method


        private Core.Settings m_Settings;
        private Core.GuiElements m_GuiElements;
        private Utilities.FileSystemUtilities m_FileSystemUtilities;
        private bool m_bPlayListLoaded = false;
        private bool m_bDirectoryChoosen = false;
        private Core.ErrorLogUpdater m_ErrorLogUpdater;

        //-------------------------------------------------------------------//
        //-------------------------function definitions----------------------//
        //-------------------------------------------------------------------//

        //-------------------------------------------------------------------//
        // Description: ...
        public MainForm()
        {
            InitializeComponent();

            m_Settings              = new Core.Settings();
            m_GuiElements           = new Core.GuiElements();
            m_FileSystemUtilities   = new Utilities.FileSystemUtilities(this);
            methodUpdateProgressbar = new MethodUpdateProgressBar(IncreaseCopyProgressBar);
            m_ErrorLogUpdater       = new Core.ErrorLogUpdater();

            m_ErrorLogUpdater.ConnectListBox(lstBoxLog);
            Core.ErrorMgmt.ConnectErrorLogUpdater(m_ErrorLogUpdater);
            Core.ErrorMgmt.ConnectMainForm(this);

        }

        //-------------------------------------------------------------------//
        // Description: ...
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        //-------------------------------------------------------------------//
        // Description: ...
        public void IncreaseCopyProgressBar(int value)
        {
            Int32 lNewProgressBarValue = progressBarFileCopy.Value + value;

            if (lNewProgressBarValue > 100)
            {
                progressBarFileCopy.Value = progressBarFileCopy.Value = 100;
            }
            else
            {
                progressBarFileCopy.Value += value;
            }
        }

        //-------------------------------------------------------------------//
        // Description: ...
        private void btnLoadPlayList_Click_1(object sender, EventArgs e)
        {
            Stream stream = m_GuiElements.ShowOpenFileDialog();
            if (null != stream)
            {
                m_Settings.m_FileList = new Parsing.ParserDelegator(stream).GetFileList();
                txtBoxPlaylistFile.Text = m_GuiElements.GetOpenFileDialog().FileName;
                m_bPlayListLoaded = true;
            }
        }

        //-------------------------------------------------------------------//
        // Description: ...
        private void btnSelectDestDir_Click(object sender, EventArgs e)
        {
            String strTargetDir = m_GuiElements.ShowOpenFolderBrowserDialog();
            if (null != strTargetDir)
            {
                m_Settings.m_strTargetDir = strTargetDir;
                txtBoxDestinationDir.Text = m_GuiElements.GetFolderBrowserDialog().SelectedPath;
                m_bDirectoryChoosen = true;
            }
        }

        //-------------------------------------------------------------------//
        // Description: ...
        private void btnRunCopyJob_Click(object sender, EventArgs e)
        {
            if (m_bPlayListLoaded && m_bDirectoryChoosen)
            {
                m_FileSystemUtilities.CopyFileListToDirectory(m_Settings.m_FileList, m_Settings.m_strTargetDir);
            }
            else
            {
                // Add specific error and do nothing
                if (!m_bPlayListLoaded)
                {
                    Core.ErrorMgmt.Add(new Core.ErrorMsg("No playlist loaded"));
                }
                if (!m_bDirectoryChoosen)
                {
                    Core.ErrorMgmt.Add(new Core.ErrorMsg("No target directory choosen"));
                }
            }
        }


    }
}
